package com.beaven.imserver.net.bean.db;

import java.time.LocalDateTime;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

/**
 * author: Beaven
 * time: 2017/5/17 14:54
 * <p>
 * 群组表
 */
@Entity
@Table(name = "TB_GROUP")
public class Group {

  @Id
  @PrimaryKeyJoinColumn
  // 主键生成存储的类型为UUID，自动生成UUID
  @GeneratedValue(generator = "uuid")
  // 把uuid的生成器定义为uuid2，uuid2是常规的UUID toString
  @GenericGenerator(name = "uuid", strategy = "uuid2")
  // 不允许更改，不允许为null
  @Column(updatable = false, nullable = false)
  private String id;

  // 群名称
  @Column(nullable = false)
  private String name;

  // 群的创建者,多个群对应一个用户
  // optional: 可选为false，必须有一个创建者
  // fetch: 加载方式FetchType.EAGER，急加载，
  // 意味着加载群的信息的时候就必须加载owner的信息
  // cascade：联级级别为ALL，所有的更改（更新，删除等）都将进行关系更新
  @ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,
      CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
  @JoinColumn(name = "ownerId")
  private User owner;
  @Column(nullable = false, updatable = false, insertable = false)
  private String ownerId;

  // 定义为创建时间戳，在创建时就已经写入
  @CreationTimestamp
  @Column(nullable = false)
  private LocalDateTime createAt = LocalDateTime.now();

  // 定义为更新时间戳，在创建时就已经写入
  @UpdateTimestamp
  @Column(nullable = false)
  private LocalDateTime updateAt = LocalDateTime.now();

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public User getOwner() {
    return owner;
  }

  public void setOwner(User owner) {
    this.owner = owner;
  }

  public String getOwnerId() {
    return ownerId;
  }

  public void setOwnerId(String ownerId) {
    this.ownerId = ownerId;
  }

  public LocalDateTime getCreateAt() {
    return createAt;
  }

  public void setCreateAt(LocalDateTime createAt) {
    this.createAt = createAt;
  }

  public LocalDateTime getUpdateAt() {
    return updateAt;
  }

  public void setUpdateAt(LocalDateTime updateAt) {
    this.updateAt = updateAt;
  }
}
